package com.lanou3g.zjl.chating.volley;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.LruCache;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.lanou3g.zjl.chating.R;

/**
 * Created by Risky on 15/10/21.
 */
public class VolleySingleton {

    private RequestQueue queue;
    private ImageLoader imageLoader;
    private Context context;
    private static VolleySingleton volleySingleton;

    //构造方法初始化请求队列和ImageLoader
    private VolleySingleton(Context context) {
        this.context = context;
        queue = getQueue();
        //初始化ImageLoader
        imageLoader = new ImageLoader(queue, new ImageLoader.ImageCache() {

            LruCache<String,Bitmap> cache = new LruCache<String,Bitmap>(10*1024*1024);

            @Override
            public Bitmap getBitmap(String url) {

                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {


                cache.put(url,bitmap);
            }
        });

    }
    //获取本类对象方法
    public static VolleySingleton getInstance(Context context) {
        if (volleySingleton == null) {
            volleySingleton = new VolleySingleton(context);
        }

        return volleySingleton;
    }
    //获取请求队列的方法
    private RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(context);
        }
        return queue;
    }

    public static final String TAG = "VolleySingleton";
    //将请求添加到队列
    public <T> void addRequest(Request<T> request) {
        request.setTag(TAG);
        queue.add(request);
    }

    public <T> void addRequest(Request<T> request, Object tag) {
        request.setTag(tag);
        queue.add(request);
    }

    public void addRequest(String url, Response.Listener<String> listener,
                           Response.ErrorListener error) {
        StringRequest stringRequest = new StringRequest(url, listener, error);
        addRequest(stringRequest);
    }

    public <T> void addRequest(String url, Class<T> clazz, Response.Listener<T> success, Response.ErrorListener failed) {
        GsonRequest<T> request = new GsonRequest<T>(url, clazz, success, failed);
        addRequest(request);
    }
    //将请求移除队列
    public void removeRequest(Object tag) {

        queue.cancelAll(tag);
    }
    //获取ImageLoader对象
    public ImageLoader getImageLoader() {
        return imageLoader;
    }

    /*
    * 从网上获取图片的方法
    * url 图片网址
    * imageView 显示图片的控件
    * */
    public void getPic(String url,ImageView imageView)
    {
        //后面两个参数为加载失败的默认图片
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(imageView, R.mipmap.default_head,R.mipmap.default_head);
        imageLoader.get(url, listener);
    }
}
